// Analysis for the state size paper
// LEC  Feb. 2, 2021
// Prepares new bidging data for R routines
// Rev. March 26, 2021
// Version adapted for Abramsonmax

cd $OUTDIR


use analysis_data, clear



global TEX 1

if ($TEX) { 
	global FMAIN  "_cox_main_stata.tex, replace "
	global FEVENT "_cox_events_stata.tex, replace "
	global FEVENT2 "_cox_events2_stata.tex, replace "
	global FEVENT3 "_cox_events3_stata.tex, replace "
	global FOLS "_ols_main.tex, replace "
	global FPOST "_cox_post45.tex, replace "
	global WIMFEIN "_cox_wimfein.tex, replace "

} 
else {
	global FMAIN "_tab.txt, replace "
	global FEVENT "_tab.txt, append "
	global FEVENT2 "_tab.txt, append "
	global FEVENT3 "_tab.txt, append "
	global FOLS "_tab.txt, append "
	global FPOST "_tab.txt, append "
	global WIMFEIN "_tab.txt, append "
}

cd $OUTPUTDIR

label variable lnetcgain "Net terr. gain, log"
label variable lef "Ethnic frac."
label variable lagfrac "Terr. frac."
label variable llarea "State area, log"
label variable llage "State age, log"
label variable llelevmean "Elev. mean, log"
label variable llelevsd "Elev. SD, log"
label variable llpop "State population, log"
label variable lcumulethsec "# eth. secession"
label variable lcumulnethsec "# non-eth. secession"
label variable lcumulethabsorb "# unific."
label variable lcumulnethabsorb "# conquests"

xtset id year
gen period = int(year/10)

gen larea = l5.area




gen gain1 = 0
replace gain1 = 1 if netgain>0

gen loss1 = 0
replace loss1 = 1 if netgain<0



// continuous DV for gain and loss

gen lngain = .
replace lngain = log(netgain+1) 
replace lngain = 0 if netgain < 0 & netgain!=.

gen lnloss = .
replace lnloss = log(abs(netgain)+1) 
replace lnloss = 0 if netgain > 0 & netgain!=.

gen event = .
replace event = 0 if netgain==0
replace event = 1 if netgain!=0 & netgain!=.

btscs event year idc, gen(dur) nspline(3)
rename _spline1  espline1
rename _spline2  espline2
rename _spline3  espline3



// DEFINE SPELLS

xtset idc year
gen spellonset = 0
replace spellonset = 1 if l5.area!=l10.area
bys idc (year): gen spellnum = sum(spellonset)
gen idstr = string(idc)+"."+string(spellnum)
egen spell = group(idstr)

bys spell: egen minspellyear = min(year)
gen spellyear = year - minspellyear + 1



//// event models, first define dichtomous DVs

gen absorb1 = 0
replace absorb1 = 1 if absorba>0 & absorba!=.

gen absorb1b = 0
replace absorb1b = 1 if absorbb>0 & absorbb!=.

gen unif1 = 0
replace unif1 = 1 if ethabsorba>0 & ethabsorba!=.

gen sec1 = 0
replace sec1 = 1 if secessionb>0 & secessionb!=.
replace sec1 = 1 if collapseb>0 & collapseb!=.


gen ethsec1 = 0
replace ethsec1 = 1 if ethsecb>0 & ethsecb!=.
replace ethsec1 = 1 if ethcollapseb>0 & ethcollapseb!=.
 
gen transf1 = 0
replace transf1 = 1 if transfera>0 & transfera!=.

gen irr1 = 0
replace irr1 = 1 if irra>0 & irra!=. 


gen unif1b = 0
replace unif1b = 1 if ethabsorbb>0 & ethabsorbb!=.

gen irr1b = 0
replace irr1b = 1 if irrb>0 & irrb!=.

// Defining duration variables

btscs ethsec1 year idc, gen(dur_ethsec) nspline(3)
rename _spline1  spline1ethsec
rename _spline2  spline2ethsec
rename _spline3  spline3ethsec

btscs unif1 year idc, gen(dur_unif) nspline(3)
rename _spline1  spline1unif
rename _spline2  spline2unif
rename _spline3  spline3unif

btscs irr1 year idc, gen(dur_irr) nspline(3)
rename _spline1  spline1irr
rename _spline2  spline2irr
rename _spline3  spline3irr



bys idc (year): gen lcumulloss = sum(l.loss1)
gen losshist1 = 0
replace losshist1 = 1 if lcumulloss > 0 & lcumulloss != .

bys idc (year): gen lcumulgain = sum(l.gain1)
gen gainhist1 = 0
replace gainhist1 = 1 if lcumulgain > 0 & lcumulgain != .

gen ethsechist1 = 0
replace ethsechist1 = 1 if lcumulethsec > 0 & lcumulethsec != .

gen unifhist1 = 0
replace unifhist1 = 1 if lcumulethabsorb > 0 & lcumulethabsorb != .

gen irrhist1 = 0
replace irrhist1 = 1 if lcumulirr > 0 & lcumulirr != .


// DURATION MODELS

// PREPARE TABLE A19

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

/*
 gen sample2 = e(sample)
stset spellyear, id(spell) failure(loss1==1) scale(1) 
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
 margins, pred(xb) at(lef=(0(0.05)1) lagfrac=0 llage=3.6 llpop=14.6 llarea=10.4  llelevmean=5.688389 llelevsd=5.06 spellnum=4)
 marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)

 stset spellyear, id(spell) failure(gain1==1) scale(1) 
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
 margins, predict(xb) at(lagfrac=(0(0.05)1) lef=0.16 llage=3.4 llpop=14.22 llarea=10.16  llelevmean=5.688389 llelevsd=4.955033)
 marginsplot, xlab(0(0.1)1) recast(line) recastci(rarea)
*/

// OUTPUT TABLE A19:  cshp_cox_events_stata.tex
esttab m1 m2 m3 m4 using $PREFIX$FMAIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1490-1790, Cox proportional hazard models\label{tab:cox:main:$PREFIX}) ///
   scalars("strat Strata")
   

/*
   
eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)  efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// esttab m1 m2 m3 m4 m5 m6 using brdg_tab.txt /*brdg_cox_events_stata.txt*/, append b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
esttab m1 m2 m3 m4 m5 m6 using $PREFIX$FEVENT b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Eth. Secession" "Eth. Secession" "Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Border-change events, Europe 1490-1790, Cox proportional hazard models \label{tab:cox:events:$PREFIX}) ///
   scalars("strat Strata")
 

 
 
 eststo clear   


stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1

stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// esttab m1 m2 m3 m4 using brdg_tab.txt /*brdg_cox_events2_stata.txt*/, append b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
esttab m1 m2 m3 m4 using $PREFIX$FEVENT2 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
    mtitles("Unification" "Unification" "Irredentism" "Irredentism") ///
   title(Ethnic border-change events (State B), Europe 1490-1790, Cox proportional hazard models \label{tab:cox:events2:$PREFIX}) ///
      scalars("strat Strata")
 
 
 
eststo clear   

stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc)  efron
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)
estadd local strat "Yes"
estimates store m6

// esttab m1 m2 m3 m4 m5 m6 using brdg_tab.txt /*brdg_cox_events3_stata.txt*/, append b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
esttab m1 m2 m3 m4 m5 m6 using $PREFIX$FEVENT3 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
    mtitles("Secession" "Secession" "Absorption" "Absorption" "Irredentism" "Irredentism") ///
   title(All border-change events, Europe 1490-1790, Cox proportional hazard models \label{tab:cox:events3:$PREFIX}) ///
       scalars("strat Strata")  
   
   
 


 
 xxx
 
 
eststo clear

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m1

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
estimates store m2

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m3

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FOLS b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Log Losses and gains, Europe 1490-1790, OLS \label{tab:ols:main:$PREFIX}) ///
   scalars("geocontrols Controls" "yearfe Year FE" "statefe State FE")
 
/*

// MAIN DURATION MODELS: PRE-/POST 1945 split
 
// Make post45 var
gen post45 = 0
replace post45 = 1 if year > 1945
lab var post45 "Post-45 (0/1)"
gen lef45 = post45 * lef
lab var lef45 "Ethnic frac. x post-45"
gen lagfrac45 = post45 * lagfrac
lab var lagfrac45 "Terr. frac. x post-45"

// Estimation
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FPOST b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac post45 lef45 lagfrac45) ///
 keep(lef lagfrac post45 lef45 lagfrac45) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1816-2017, Cox proportional hazard models: Pre- vs. post-1945\label{tab:cox:post45:$PREFIX}) ///
   scalars("strat Strata")
   
   
 
   
   
   
// MAIN DURATION MODELS: NATIONALIST ORGS from Wimmer & Feinstein
 
// Make interaction vars

lab var natorgyesno "Nationalist org."
gen lefnatorg = natorgyesno * lef
lab var lefnatorg "Ethnic frac. x nat. org."
gen lagnatorg = natorgyesno * lagfrac
lab var lagnatorg "Terr. frac. x nat. org."

// Estimation
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$WIMFEIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
 keep(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, Europe 1816-2017, Cox proportional hazard models: Nationalist Organizations\label{tab:cox:wimfein:$PREFIX}) ///
   scalars("strat Strata")
   
*/   

*/
   
